package jgl.vajra.management.widget.retrofithelper.rxexception;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.functions.Function;

/**
 * Created by Administrator on 2016/11/24 0024.
 */
public class RxExceptionHelper {
    private static final ObservableTransformer ioTransformer = new ObservableTransformer() {

        @Override
        public ObservableSource apply(Observable upstream) {
            return upstream.retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
                @Override
                public ObservableSource<?> apply(Observable<Throwable> throwableObservable) throws Exception {
                    return throwableObservable.flatMap(new Function<Throwable, ObservableSource<?>>() {
                        @Override
                        public ObservableSource<?> apply(Throwable throwable) {
                            if (throwable instanceof TokenInvalidException) {
                                return Observable.error(throwable);
                            } else {
                                return Observable.error(throwable);
                            }

                        }
                    });
                }
            });
        }
    };

    public static ObservableTransformer applyTransformer() {
        return ioTransformer;
    }
}
